﻿using HlELock.Core.Events;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using Syncfusion.Windows.Shared;
using System.Windows;

namespace HlELock.Core
{
    /// <summary>
    /// DialogWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DialogWindow : ChromelessWindow
    {
        private readonly DialogWindowEventArgs _dialogWindowEventArgs;
        private readonly IRegionManager _regionManager;
        public DialogWindow(IRegionManager regionManager, Window owner, DialogWindowEventArgs args)
        {
            DataContext = DialogWindowViewModel.Instance;
            _dialogWindowEventArgs = args;

            RegionManager.SetRegionManager(this, _regionManager);

            InitializeComponent();
            Title = args.Title;
            Owner = owner;
            _regionManager = regionManager;
            Loaded += DialogWindow_Loaded;
        }
        private void DialogWindow_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            var p = new NavigationParameters();
            if (_dialogWindowEventArgs.Parameters != null)
            {
                foreach (var item in _dialogWindowEventArgs.Parameters)
                {
                    p.Add(item.Key, item.Value);
                }
            }
            _regionManager.RequestNavigate(RegionNames.DialogWindowRegion, _dialogWindowEventArgs.Uri, p);

            EventBus.Instace.GetEvent<CloseDialogWindowEvent>().Subscribe(args =>
            {
                this.Close();
            }, ThreadOption.UIThread);
        }
    }
}
